package com.example.crawler.controller;

import com.example.crawler.mapper.HuShenMapper;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.*;

@Controller
@RequestMapping("/hs/v1/")
public class HuShenController {

    @Resource
    private HuShenMapper huShenMapper;

    @GetMapping(value = "/getHs")
    @ApiOperation("爬取沪深拟上市数据")
    public void getHs() throws IOException {
        // 网址
        List<String> urlList = new ArrayList<>();
        for (int i = 1; i <= 50; i++){
            String url = "http://eid.csrc.gov.cn/ipo/1010/index_" + i + ".html";
            urlList.add(url);
        }
        for (int i = 1; i <= 50; i++){
            String url = "http://eid.csrc.gov.cn/ipo/1017/index_" + i + ".html";
            urlList.add(url);
        }

        int index = 1;
        System.out.println(urlList.size());
        for (String url : urlList) {
            System.out.println(index++ + ":               " +  url);
            Document document = Jsoup.parse(new URL(url), 300000);
            Elements elements = document.getElementsByClass("m-table2 m-table2-0");
            Element el = elements.first();
            List<Map<String, String>> tableColumnData = getTableColumnData(el, "1");
            // System.out.println(JSONObject.toJSONString(tableColumnData));
            for (Map<String, String> tableColumnDatum : tableColumnData) {
                huShenMapper.insert(tableColumnDatum);
            }

        }
        System.out.println("爬取结束！");

    }

    /**
     * @param table
     * @param rowSelectRange
     * @return
     */
    public List<Map<String, String>> getTableColumnData(Element table, String rowSelectRange) {
        List<Map<String, String>> companyList = new ArrayList<>();

        Elements trs = table.select("tr");
        if (!StringUtils.isEmpty(rowSelectRange)) {
            String[] deleteRows = rowSelectRange.split(",");
            int offsetIndex = 0;
            for (int i = deleteRows.length - 1; i >= 0; i--) {
                int index = Integer.parseInt(deleteRows[i]);
                if (index < 0) {
                    index = Math.abs(index);
                    index = trs.size() - (index - offsetIndex);
                    trs.remove(index);
                    offsetIndex++;
                } else {
                    trs.remove(index - 1);
                }
            }
        }
        for (Element tr : trs) {
            Elements tds = tr.select("td");
            // th 和 td 混合的情况下，取子元素
            if (tr.select("th").size() > 0) {
                tds = tr.children();
            }
            int index = 0;
            Map<String, String> map = new HashMap<>();
            for (Element td : tds) {
                switch (index++) {
                    case 0:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("pname", td.text().trim());
                        break;
                    case 1:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("ptype", td.text().trim());
                        break;
                    case 2:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("bankuai", td.text().trim());
                        break;
                    case 3:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("jigou", td.text().trim());
                        break;
                    case 4:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("shijian", td.text().trim());
                        break;
                    case 5:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("gonggao", td.text().trim());
                        break;
                }
            }
            companyList.add(map);
        }
        return companyList;
    }


}
